package cn.myframe.task.init;

import cn.myframe.entity.sys.SysBaseDataEntity;
import cn.myframe.service.sys.SysBaseDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component(value = "bseDataInit")
public class BaseDataInit {

    @Autowired
    private SysBaseDataService sysBaseDataService;

    public static Map<String,Map<String, List<SysBaseDataEntity>>> I18n_Date_MAP=new HashMap<String,Map<String, List<SysBaseDataEntity>>>();

    @PostConstruct
    public void getAllBaseData(){
        List<SysBaseDataEntity> baseDataList = sysBaseDataService.selectAllBaseData();
        List<String> allType = sysBaseDataService.selectAllType();
        List<String> i18n = sysBaseDataService.selectI18n();
        for(SysBaseDataEntity baseData : baseDataList){
            for(String language : i18n){
                if(I18n_Date_MAP.containsKey(language)){
                    putTypeData( baseData ,  allType,  language);
                }else{
                    Map<String, List<SysBaseDataEntity>> dataMap = new HashMap<String, List<SysBaseDataEntity>>();
                    I18n_Date_MAP.put(language, dataMap);
                    putTypeData( baseData ,  allType,  language);
                }
            }
        }
    }

    public void putTypeData(SysBaseDataEntity baseData , List<String> types, String language){
        Map<String, List<SysBaseDataEntity>> dataMap = I18n_Date_MAP.get(language);
        if(baseData.getLanguage().equals(language)){
            for(String type : types){
                if(dataMap.containsKey(type)){
                    if(baseData.getType().equals(type)){
                        dataMap.get(type).add(baseData);
                        break;
                    }
                }else{
                    List<SysBaseDataEntity> list = new ArrayList<>();
                    dataMap.put(type, list);
                    if(baseData.getType().equals(type)){
                        list.add(baseData);
                        break;
                    }
                }
            }
        }
    }
}
